from pymongo import MongoClient
import pymongo
import os
import json
import csv
client = MongoClient(port=27017)
db=client['tiktok']
labels={}
with open('D:\\Work\\Tool\\tiktok\\all\\infos_sp.tsv', 'r', encoding='utf-8',
          newline='\n') as filename_input:
    reader = csv.reader(filename_input, delimiter='\t')
    next(reader)
    for line in reader:
        try:
            labels[line[1]]=line[17]
        except:
            print(line[1])
collist=['cancelthenoise', 'itwasntme', 'beatsdaisychallenge', 'monclerbubbleup', 'airpodsjump', 'inmyaejeans', 'thisisbliss', 'gotmilkchallenge', 'expressieyourself', 'dopacsun', 'strictlycurls', 'hereforrmhc', 'boseallout', 'moodflip', 'whatsyourpower', 'asosfashunweek', 'letsfaceit', 'showmeyourwalk', 'scoobdance', 'merrybossmas']

for col in collist:
    print('updating ',col)
    t=0
    for item in db[col].find():
        if 'video_len' in item['video_feature']['editing'].keys():
            continue
        if item['_id'] not in labels.keys():
            continue
        vl=int(labels[item['_id']])
        if vl>60:
            vl=60
        if vl<=0:
            vl=3
        db[col].update_one({'_id': item['_id']}, {'$set': {'video_feature.editing': {'video_len':vl}}})
        t+=1
    print('updated ',t)

